package br.com.ngame.manager;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Vector;

import javax.imageio.ImageIO;

import br.com.ngame.device.VideoDevice;

public class WindowedImageManager implements ImageManager {

    protected HashMap<String, Vector<BufferedImage>> collections;
    protected VideoDevice videoDevice;
    private boolean imagesInResource = true;

    private WindowedImageManager() {
    }

    public WindowedImageManager(VideoDevice videoDevice) {
        this.videoDevice = videoDevice;
        collections = new HashMap<String, Vector<BufferedImage>>();
    }

    public void loadImageCollections(String[] fileNames) {
        for (int i = 0; i < fileNames.length; i++) {
            loadImageCollection(fileNames[i]);
        }
    }

    public void loadImageCollection(String fileName) {
        Vector<BufferedImage> vect = new Vector<BufferedImage>();
        try {
            doLoadImageSheet(vect, fileName);
            collections.put(fileName, vect);
        } catch (IOException ioe) {
            System.err.println(ioe.getClass().getName() + ": " + ioe.getMessage());
        }
    }

    public void loadImage(String fileName) {
        try {
            BufferedImage img = doLoadImage(fileName);
            Vector<BufferedImage> vect = new Vector<BufferedImage>();
            vect.add(img);
            collections.put(fileName, vect);
        } catch (IOException ioe) {
            System.err.println(ioe.getClass().getName() + ": " + ioe.getMessage());
        }
    }

    public BufferedImage getImage(String fileName, int imageIndex) {
        return collections.get(fileName).get(imageIndex);
    }

    public BufferedImage getImage(String fileName) {
        return collections.get(fileName).get(0);
    }

    public int getImageCount(String fileName) {
        return collections.get(fileName).size();
    }

    protected void doLoadImageSheet(Vector<BufferedImage> vect, String fileName)
        throws IOException {
        BufferedImage imageFile = doLoadImage(fileName);
        int imageSize = imageFile.getHeight();
        int nImages = imageFile.getWidth() / imageSize;
        for (int i = 0; i < nImages; i++) {
            BufferedImage tmp = videoDevice.newImage(imageSize, imageSize);
            tmp.getGraphics().drawImage(imageFile, 0, 0, imageSize, imageSize, imageSize * i, 0, imageSize * i + imageSize, imageSize, null);
            vect.add(tmp);
        }
    }

    protected BufferedImage doLoadImage(String fileName)
        throws IOException {
        BufferedImage img = null;
        if (isImagesInResource()) {
            img = ImageIO.read(getClass().getResource("/" + fileName));
        } else {
            img = ImageIO.read(new File(fileName));
        }
        return img;
    }

    public boolean isImagesInResource() {
        return imagesInResource;
    }

    public void setImagesInResource(boolean value) {
        imagesInResource = value;
    }
}